package cn.roadtrip.mtravelbusinessmodule.bs.travelline;

import cn.roadtrip.mtravelbusinessmodule.sql.couple.travelline.dto.BsHotelRoomPriceDTO;
import cn.roadtrip.mtravelbusinessmodule.sql.couple.travelline.dto.BsPreTravelCarPriceDTO;
import cn.roadtrip.mtravelbusinessmodule.sql.couple.travelline.tables.BsHotelroomprice;
import cn.roadtrip.mtravelbusinessmodule.sql.couple.travelline.tables.BsPretravelcarprice;
import cn.roadtrip.mtravelbusinessmodule.sql.couple.travelline.tables.records.BsHotelroompriceRecord;
import cn.roadtrip.mtravelbusinessmodule.sql.couple.travelline.tables.records.BsPretravelcarpriceRecord;
import cn.roadtrip.utilmodule.util.DataBean;
import cn.roadtrip.utilmodule.util.enumpck.Enum_CarDetailType;
import cn.roadtrip.utilmodule.util.enumpck.Enum_HotelRoomType;
import com.github.yitter.idgen.YitIdHelper;
import com.google.gson.Gson;
import lombok.*;
import org.jooq.DSLContext;
import org.jooq.impl.DSL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.stream.Collectors;

/**
 * 预置金额相关操作
 */
@Component
public class PrePriceSettingImpl {
    @Autowired
    private DSLContext dslContext;



    @Data
    @Builder
    @AllArgsConstructor
    @NoArgsConstructor
    @EqualsAndHashCode
    public static class Model_getPreCarPriceList {
        private Integer dayNum;//游玩天数
    }

    /**
     * 获取车辆类型以及价格
     *
     * @return
     */
    public DataBean getPreCarPriceList(Model_getPreCarPriceList model) {
        java.util.List<BsPreTravelCarPriceDTO> list = new ArrayList<>();
        try {
            list = dslContext.selectFrom(BsPretravelcarprice.BS_PRETRAVELCARPRICE)
                    .where(
                            BsPretravelcarprice.BS_PRETRAVELCARPRICE.TRASH.eq(false)
                                    .and((model == null || model.getDayNum() == null) ? DSL.noCondition() : BsPretravelcarprice.BS_PRETRAVELCARPRICE.DAYNUM.eq(model.getDayNum()))
                    )
                    .fetch().into(BsPreTravelCarPriceDTO.class).stream().sorted(Comparator.comparing(BsPreTravelCarPriceDTO::getLevelType)).collect(Collectors.toList());
        } catch (Exception e) {
            e.printStackTrace();
            return DataBean.errEmpty();
        }
        java.util.Map<String, java.util.List<BsPreTravelCarPriceDTO>> datas = list.stream().collect(Collectors.groupingBy(BsPreTravelCarPriceDTO::getLevelType));
        return DataBean.suc(datas, datas.size(), "suc", "操作成功");
    }

    @Data
    @Builder
    @AllArgsConstructor
    @NoArgsConstructor
    @EqualsAndHashCode
    public static class Model_createPreCarPric {
        private Enum_CarDetailType levelType;
        private Integer dayNum;
        private String price;
        private java.util.List<String> images;
        private java.util.Map<String,Object> extInfor;
    }

    /**
     * 创建车辆类型以及费用
     *
     * @return
     */
    public DataBean createPreCarPrice(java.util.List<Model_createPreCarPric> models) {

        if (models == null || models.isEmpty()) {
            return DataBean.err("param-empty", "参数不能为空");
        }
        java.util.Set<String> sameTypeSet = new HashSet<>();//比对同类型
        for (Model_createPreCarPric model : models) {
            sameTypeSet.add(model.getLevelType().getCode() + ":" + model.getDayNum());
        }
        if (sameTypeSet.size() != models.size()) {
            return DataBean.err("type-repeat", "同类型不能重复");
        }
        try {
            return dslContext.transactionResult(ctx -> {
                for (Model_createPreCarPric model : models) {
                    //先清除
                    dslContext.update(BsPretravelcarprice.BS_PRETRAVELCARPRICE)
                            .set(BsPretravelcarprice.BS_PRETRAVELCARPRICE.TRASH, true)
                            .where(BsPretravelcarprice.BS_PRETRAVELCARPRICE.TRASH.eq(false
                                            )
                                            .and(
                                                    BsPretravelcarprice.BS_PRETRAVELCARPRICE.LEVELTYPE.eq(model.getLevelType().getCode())
                                            ).and(
                                                    BsPretravelcarprice.BS_PRETRAVELCARPRICE.DAYNUM.eq(model.getDayNum())
                                            )
                            )
                            .execute();
                    //新增操作
                    if(model.getImages()==null){
                        model.setImages(new ArrayList<>());
                    }
                    if(model.getExtInfor()==null){
                        model.setExtInfor(new HashMap<>());
                    }
                    Gson gson = new Gson();
                    dslContext.insertInto(BsPretravelcarprice.BS_PRETRAVELCARPRICE)
                            .set(new BsPretravelcarpriceRecord(
                                    YitIdHelper.nextId() + "",
                                    false,
                                    model.getLevelType().getCode(),
                                    model.getDayNum(),
                                    new BigDecimal(model.getPrice()),
                                    System.currentTimeMillis(),
                                    model.getLevelType().getTitle(),
                                    gson.toJson(model.getImages()),
                                    gson.toJson(model.getExtInfor())
                            )).execute();
                }
                dslContext.commit();
                return DataBean.suc(true, 1, "suc", "创建成功");
            });
        } catch (Exception e) {
            e.printStackTrace();
            return DataBean.err("create-error", "创建失败");
        }

    }

    @Data
    @Builder
    @AllArgsConstructor
    @NoArgsConstructor
    @EqualsAndHashCode
    public static class Model_getPreHotelRoomTypePriceList {
        private Integer dayNum;//游玩天数
    }

    /**
     * 获取房间类型
     *
     * @return
     */
    public DataBean getPreHotelRoomTypePriceList(Model_getPreHotelRoomTypePriceList model) {
        java.util.List<BsHotelRoomPriceDTO> list = new ArrayList<>();
        try {
            list = dslContext.selectFrom(BsHotelroomprice.BS_HOTELROOMPRICE)
                    .where(
                            BsHotelroomprice.BS_HOTELROOMPRICE.TRASH.eq(false)
                                    .and((model == null || model.getDayNum() == null) ? DSL.noCondition() : BsHotelroomprice.BS_HOTELROOMPRICE.DAYNUM.eq(model.getDayNum()))
                    )
                    .fetch().into(BsHotelRoomPriceDTO.class).stream().sorted(Comparator.comparing(BsHotelRoomPriceDTO::getLevelType)).collect(Collectors.toList());
        } catch (Exception e) {
            e.printStackTrace();
            return DataBean.errEmpty();
        }
        java.util.Map<String, java.util.List<BsHotelRoomPriceDTO>> datas = list.stream().collect(Collectors.groupingBy(BsHotelRoomPriceDTO::getLevelType));
        return DataBean.suc(datas, datas.size(), "suc", "操作成功");
    }

    @Data
    @Builder
    @AllArgsConstructor
    @NoArgsConstructor
    @EqualsAndHashCode
    public static class Model_createHotelRoomTypePrice {
        private Enum_HotelRoomType levelType;
        private Integer dayNum;
        private String price;
        private java.util.List<String> images;
        private java.util.Map<String,Object> extInfor;
    }

    /**
     * 创建住房
     *
     * @return
     */
    public DataBean createHotelRoomTypePrice(
            java.util.List<Model_createHotelRoomTypePrice> models
    ) {
        if (models == null || models.isEmpty()) {
            return DataBean.err("param-empty", "参数不能为空");
        }
        java.util.Set<String> sameTypeSet = new HashSet<>();//比对同类型
        for (Model_createHotelRoomTypePrice model : models) {
            sameTypeSet.add(model.getLevelType().getCode() + ":" + model.getDayNum());
        }
        if (sameTypeSet.size() != models.size()) {
            return DataBean.err("type-repeat", "同类型不能重复");
        }
        try {
            return dslContext.transactionResult(ctx -> {
                for (Model_createHotelRoomTypePrice model : models) {
                    //先清除
                    dslContext.update(BsHotelroomprice.BS_HOTELROOMPRICE)
                            .set(BsHotelroomprice.BS_HOTELROOMPRICE.TRASH, true)
                            .where(BsHotelroomprice.BS_HOTELROOMPRICE.TRASH.eq(false
                                            )
                                            .and(
                                                    BsHotelroomprice.BS_HOTELROOMPRICE.LEVELTYPE.eq(model.getLevelType().getCode())
                                            ).and(
                                                    BsHotelroomprice.BS_HOTELROOMPRICE.DAYNUM.eq(model.getDayNum())
                                            )
                            )
                            .execute();
                    //新增操作
                    if(model.getImages()==null){
                        model.setImages(new ArrayList<>());
                    }
                    if(model.getExtInfor()==null){
                        model.setExtInfor(new HashMap<>());
                    }
                    Gson gson = new Gson();
                    dslContext.insertInto(BsHotelroomprice.BS_HOTELROOMPRICE)
                            .set(new BsHotelroompriceRecord(
                                    YitIdHelper.nextId() + "",
                                    false,
                                    model.getLevelType().getCode(),
                                    model.getDayNum(),
                                    new BigDecimal(model.getPrice()),
                                    System.currentTimeMillis(),
                                    model.getLevelType().getTitle(),
                                    gson.toJson(model.getImages()),
                                    gson.toJson(model.getExtInfor())
                            )).execute();
                }
                dslContext.commit();
                return DataBean.suc(true, 1, "suc", "创建成功");
            });
        } catch (Exception e) {
            e.printStackTrace();
            return DataBean.err("create-error", "创建失败");
        }

    }
}
